/*
专家学者详情页面 JS
 */

function getUrlByKey(key) {
    let result = '';
    let apiRoot = "/v1/";
    $.ajax({
        method: 'GET',
        url: apiRoot,
        dataType: 'json',
        async: false, // 同步
        success: function (data) {
            result = data[key];
        }
    });
    return result;
}

/**
 * 为专家详情页面标签赋值，使用 id 查询 author
 */
function setAuthorDetailPage() {
    // let id = getQueryString('id');
    let params = location.search;
    if (params.indexOf("id=") == null) return;
    // TODO 专家详情页面赋值
    let authorUrl = getUrlByKey('authors') + getQueryString('id');
    $.ajax({
            method: 'GET',
            url: authorUrl,
            dataType: 'json',
            success: function (data) {
                let author = data;
                $('#aid').val(author['id']);
                $('#author-name').html(author['name']);
                if (author['icon'] != null)
                    $('#author-icon').attr('src', author['icon']);
                // $('#ch-name').val(authors[0].cname)
                let orgs = author['orgs'];
                if(orgs){
                    $('#org').empty();
                    for(let i = 0; i < orgs.length; i++) {
                        $('#org').append(orgs[i] + ";<br/>");
                    }
                }
                $('#position').html(authors['position']);
                if (author['n_pubs'] != null)
                    $('#pubs-count').html(author['n_pubs']);
                for (let i = 0; i < author['tags'].length; i++) {
                    $('#author-tags').append(author['tags'][i]);
                    if (i != author['tags'].length - 1)
                        $('#author-tags').append('、');
                }
                if (author['pubs']){
                    setPaper(author['pubs']);
                }
            }
        }
    );
}

/*
<li>
1 <div class="single-timeline-box fix row">

2  <div class="col-md-2 pubs-time text-right">
    <h2>2018 - Present</h2>
    <h3>creative director</h3>
</div><!--/.col-->

3 <div class="col-md-offset-1 col-md-5">
4    <div class="timeline timeline-content">
        <h4 class="title">
            <span><i class="fa fa-circle" aria-hidden="true"></i></span>
            hoplony tech limited
        </h4>
        <h5>newyork, USA</h5>
        <p class="description">
            Duis aute irure dolor in reprehenderit in vol patate velit esse cillum
            dolore eu fugiat nulla pari. Excepteur sint occana inna tecat cupidatat
            non proident.
        </p>
    </div><!--/.timeline-->
</div><!--/.col-->
</div><!--/.single-timeline-box-->
</li>
*/
function setPaper(pubs) {
    // 添加文献
    for (let i = 0; i < pubs.length; i++) {
        let li = document.createElement("li");
        let div1 = document.createElement("div");
        let div2 = document.createElement("div");
        let div3 = document.createElement("div");
        let div4 = document.createElement("div");
        let year = document.createElement("h2");
        let authors = document.createElement("h3");
        let title = document.createElement("h4");
        let keywords = document.createElement("h5");
        let abstract = document.createElement("p");
        title.className = "title";
        abstract.className = "description ";
        div1.className = "single-timeline-box fix row";
        div2.className = "col-md-2 pubs-time text-right";
        div3.className = "col-md-offset-1 col-md-5";
        div4.className = "timeline timeline-content";
        div2.appendChild(year);
        div2.appendChild(authors);
        div4.appendChild(title);
        div4.appendChild(keywords);
        div4.appendChild(abstract);
        div3.appendChild(div4);
        div1.appendChild(div2);
        div1.appendChild(div3);
        li.appendChild(div1);
        $('#author-pubs-list').append(li);
        let paperUrl = getUrlByKey('papers') + "retreive/?id=" + pubs[i].i;
        // 获取 paper
        $.ajax({
            method: 'GET',
            url: paperUrl,
            dataType: 'json',
            success: function (data) {
                let paper = data['results'][0];
                if(paper['year'])
                    year.innerHTML = "发表年份： " + paper['year'];
                else
                    year.innerHTML = "发表年份： —";
                for (let a_num = 0; a_num < paper['authors'].length; a_num++) {
                    authors.innerHTML += paper['authors'][a_num]['name'] + "；<br/>";
                }
                title.append(paper['title']);
                if(paper['keywords'])
                    keywords.innerHTML = "关键词：" + paper['keywords'].toString();
                else
                    keywords.innerHTML = "关键词： —";
                if (paper['abstract']) {
                    abstract.innerHTML = "摘要：" + paper['abstract'].substr(0, 70);
                    if (paper['abstract'].length > 70) {
                        abstract.innerHTML += '\n' + paper['abstract'].substr(70, 140);
                    }
                }
            }
        })
    }
}

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
    if (r != null)
        return unescape(r[2]);
    return null;
}